<?php

	require_once(APPPATH . 'controllers/base_controllers/base_validation_controller.php');
	
	class Walk_registration_validation extends Base_validation_controller
	{
		
		public function __construct()
		{
			parent::__construct();
			
			$this->load->library('validation');
		}
		
		
		public function validate_step1()
		{
			//$rules['userName'] = "trim|required|callback_valid_user_name_check|max_length[32]";
			$rules['userName'] = "trim|required|max_length[32]";
			$rules['password'] = "trim|required|min_length[6]";
			$rules['firstName']	= "trim|required";
			$rules['lastName'] = "trim|required";
			$rules['gender'] = "trim|required";
			$rules['primaryEmail'] = "trim|required|valid_email";
			$rules['homePhoneNumber'] = "trim|required|callback_valid_phone_check";
			$rules['mobilePhoneNumber'] = "trim|callback_valid_phone_check";
			$rules['homeAddress1'] = "trim|required";
			$rules['city'] = "trim|required";
			$rules['state'] = "trim|required";
			$rules['zipCode'] = "trim|required|callback_valid_zip_code_check";
			
			$this->validation->set_rules($rules);
			
			$validationPassed = $this->validation->run();
			
			if($validationPassed)
			{
				echo( 'VALIDATION_SUCCESS' );
			}
			else
			{
				echo( $this->clean_validation_errors_for_step1( $this->validation->error_string ) );
			}
			
			die;
		}
		
		
		public function validate_step2()
		{
			$rules['displayName'] = "trim|required|max_length[25]";
			$rules['numWalkers'] = "trim|required|numeric";
			$rules['numRunners'] = "trim|required|numeric";
			$rules['includedTShirtSize'] = "trim|required";
			$rules['goal'] = "trim|numeric";
			$rules['donation'] = "trim|numeric";
			
			if( $this->input->post('additionalTShirts') > 0 )
			{
				for( $i = 1; $i <= $this->input->post('additionalTShirts'); $i++ )
				{
					$rules['additionalTShirtSize' . $i] = "trim|required";
				}
			}
			
			$this->validation->set_rules($rules);
			
			$validationPassed = $this->validation->run();
			
			if( $this->input->post('numWalkers') == 0 && $this->input->post('numRunners') == 0 )
			{
				$validationPassed = FALSE;
				$this->validation->error_string .= "<p>You must enter at least one walker or one runner.</p>";
			}
			
			if($validationPassed)
			{
				echo( 'VALIDATION_SUCCESS' );
			}
			else
			{
				echo( $this->clean_validation_errors_for_step2( $this->validation->error_string ) );
			}
			
			die;
		}
		
		
		
		private function clean_validation_errors_for_step1( $valErrors )
		{
			$valErrors = str_replace("userName", "User Name", $valErrors);
			$valErrors = str_replace("password", "Password", $valErrors);
			$valErrors = str_replace("firstName", "First Name", $valErrors);
			$valErrors = str_replace("lastName", "Last Name", $valErrors);
			$valErrors = str_replace("gender", "Gender", $valErrors);
			$valErrors = str_replace("primaryEmail", "Primary Email", $valErrors);
			$valErrors = str_replace("homePhoneNumber", "Home Phone Number", $valErrors);
			$valErrors = str_replace("mobilePhoneNumber", "Mobile Phone Number", $valErrors);
			$valErrors = str_replace("dateJoinedCmb", "Date Joined CMB", $valErrors);
			$valErrors = str_replace("homeAddress1", "Home Address 1", $valErrors);
			$valErrors = str_replace("city", "City", $valErrors);
			$valErrors = str_replace("state", "State", $valErrors);
			$valErrors = str_replace("zipCode", "Zip Code", $valErrors);
			
			return $valErrors;
		}
		
		
		private function clean_validation_errors_for_step2( $valErrors )
		{
			$valErrors = str_replace("individualOrFamily", "Individual or family registration", $valErrors);
			$valErrors = str_replace("displayName", "Display name", $valErrors);
			$valErrors = str_replace("numWalkers", "Number of walkers", $valErrors);
			$valErrors = str_replace("numRunners", "Number of runners", $valErrors);
			$valErrors = str_replace("includedTShirtSize", "Size for included T-Shirt", $valErrors);
			$valErrors = str_replace("hearAbout", "How did you hear about", $valErrors);
			$valErrors = str_replace("chinmayaMissionBostonMember", "Are you a member", $valErrors);
			$valErrors = str_replace("additionalTShirtSize", "Size for extra T-Shirt #", $valErrors);
			
			return $valErrors;
		}
		
		
	}

?>